<?php
	class CompanycontrolAction extends Action {
	
		
		public function __construct() {
			parent::__construct();

			if(!empty($_GET['from'])){
				$_SESSION = D('Company')->get_company_info_bymanage($_GET['from']);
			}
		}
		public function check(){
			$access_token = D('Weixin')->getaccess_login($_GET['code']);
			$openid = $access_token['openid'];
			$result = D('company_manage')->where('wx_id = "'.$openid.'" AND is_del=0')->find();
			if(!empty($result)){
				$url = U('Companycontrol/index',array('from'=>$openid));
				redirect($url);
			}else{
				$url = U('Companycontrol/nopass');
				redirect($url);
			}
		}
	
		public function index(){
			$resume_count = D('Company')->count_company_hiring();
			$this->assign('resume_count',$resume_count);
			$count = D('Company')->count_hiring_check();
			$count = array_sum($count);
			$this->assign('count',$count);
			$this->display();
		}
		
		public function hiring_list(){
			$cid = $_SESSION['company']['cid'];
			$menu = D('company_menu')->where('cid ='.$cid)->find();
			$this->assign('menu',$menu);
			$hiring_list = D('Company')->get_company_hiring_list();
			foreach($hiring_list as $k => &$v){
				$v['resume_count'] = D('Company')->count_company_resume($v['hiring_id']);
			}
			$this->assign('hiring_list',$hiring_list);
			$this->display();
		}
		
		public function hiring_show(){
			$hid = $_GET['hiring_id'];
			$list = D('Hiring')->hiring_show($hid);
			$this->assign('list',$list);
			$this->assign('workplace',format_workplace($list['store']));
			$this->display();
		}
		
		public function resume_list(){
			$send_id = $_GET['send_id'];
			$cid = $_SESSION['company']['cid'];
			$sid = $_GET['sid'];
			$reply_other = $_GET['reply_other'];
			if($reply_other == 1){
				if($sid !=""){
					$da['check'] = 1;
					D('send')->where('sid ='.$sid)->save($da);
				}
			}
			
			if($_GET['weixinmessage']){
				$_SESSION = D('Company')->get_company_info_bybid($_GET['manage']);
			}else{
				$com_info = D('company_info')->where(array('cid'=>$cid,'is_del'=>0))->find();
				$this->assign('com_info',$com_info);
			}
			$resume_list = D('Company')->get_company_resume_list($send_id,$cid);
			$this->assign('resume_list',$resume_list);
			$this->display();
		}
		
		public function hiring_add(){
			$company_info = $_SESSION['company'];
			$stores = D('Company')->list_store();
			$manage = D('Company')->get_manage_list();
			$cid = $_SESSION['company']['cid'];
			$menu = D('company_menu')->where('cid ='.$cid)->find();
			$this->assign('job_num',$menu['job_num']);
			$this->assign('manage',$manage);
			$this->assign('hangye',gethangye());
			$this->assign('edu',getedu());
			$this->assign('height',C('HEIGHT_CONFIG'));
			$this->assign('exper',C('EXPER_CONFIG'));
			$this->assign('worktime',getwork_time());
			$this->assign('workplace',format_workplace_page($stores));
			$templates = D('Company')->get_hiring_template();
			$this->assign('templates',$templates);
			$this->assign('brand',format_brand($company_info['brand']));
			$this->display();
		}
	
		public function hiring_edit() {
			$company_info = $_SESSION['company'];
			$stores = D('Company')->list_store();
			$hid = $_GET['hiring_id'];
			$list = D('Company')->get_hiring_info($hid);
			$manage = D('Company')->get_manage_list();
			$this->assign('manage',$manage);
			$this->assign('list',$list);
			$this->assign('hangye',gethangye());
			$this->assign('edu',getedu());
			$this->assign('height',C('HEIGHT_CONFIG'));
			$this->assign('exper',C('EXPER_CONFIG'));
			$this->assign('worktime',getworktime());
			$this->assign('workplace',format_workplace_page($stores));
			$position = D('position')->where('is_del = 0 AND hid = '.$list['hid'])->select();
			$this->assign('posi',$position);
			$this->assign('brand',format_brand($company_info['brand']));
			$this->display();
		}
		
		public function hiring_doadd(){
			$cid = $_SESSION['company']['cid'];
			$menu = D('company_menu')->where('cid ='.$cid)->find();
			if($menu['job_num'] == 0){
				$this->success("您添加职位的次数已经用完，请及时充值，或联系我们！");	
				exit;
			}
			$data = $_POST;
			if($data['template_id']==0){
				$data['template_id'] = D('Company')->add_hiring_template($data['template'][$data['template_id']]);
			}
			$data['template'][$data['template_id']]['hiring_id'] = D('Company')->add_hiring($data);
			$result = D('Company')->add_hiring_info($data['template'][$data['template_id']]);
			if($result){
				if($data['hiring_id']==""){
					$re1 = D('company_menu')->where('cid ='.$cid)->setDec('job_num');
					if($re1){
						$this->success("添加成功！");		
					}
				}else{
					$this->success("修改成功！");	
				}
				
			}else{
				if($data['hiring_id']==""){
					$this->success("添加失败！");	
				}else{
					$this->success("修改失败！");	
				}
			}
		}
		
		public function hiring_save(){
			$hiring_id = $_GET['hiring_id'];
			$data = $_POST;
			$data['info']['notice'] = implode(',', $data['info']['notice']);
			$result_hiring = D('Company')->save_hiring($data,$hiring_id);
			$result_hiring_info = D('Company')->save_hiring_info($data['info'],$hiring_id);
			if($result_hiring&&$result_hiring_info){
				$this->success("修改成功！");	
			}else{
				$this->error("修改失败！");	
			}
		}
		
		public function hiring_del(){
			$hiring_id = $_POST['hiring_id'];
			$data['is_del'] = 1;
			$result = D('hiring')->where('hiring_id = '.$hiring_id)->save($data);
			if($result){
				$this->ajaxReturn('','删除成功',1);
			}else{
				$this->ajaxReturn('','删除失败',0);
			}
		}
	
		public function hiring_refresh(){
			$cid = $_SESSION['company']['cid'];
			$menu = D('company_menu')->where('cid ='.$cid)->setDec('refresh');
			$hiring_id = $_POST['hiring_id'];
			$data['updateline'] = time();
			$result_hiring = D('hiring')->where('hiring_id = '.$hiring_id)->save($data);
			$result_hiring_info = D('hiring_info')->where('hiring_id = '.$hiring_id)->save($data);
			if($result_hiring&&$result_hiring_info&&$menu){
				$this->ajaxReturn('','刷新成功',1);
			}else{
				$this->ajaxReturn('','刷新失败',0);
			}
		}
		
		public function order_audition(){
			$data = $_POST;
			$data['hiring_id'] = $_GET['hiring_id'];
			$result = D('Company')->order_audition($data);
			if($result){
				$this->success('预约成功',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}else{
				$this->error('预约失败',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}
		}
		
		public function fly_resume(){
			$data = $_POST;
			$data['hiring_id'] = $_GET['hiring_id'];
			$result = D('Company')->fly_resume($data);
			if($result){
				$this->success('放鸽子成功',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}else{
				$this->error('放鸽子失败',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}
		}
		
		public function esc_resume(){
			$data = $_POST;
			$data['hiring_id'] = $_GET['hiring_id'];
			$result = D('Company')->esc_resume($data);
			if($result){
				$this->success('删除简历成功',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}else{
				$this->error('删除简历失败',U('Companycontrol/resume_list',array('hiring_id'=>$data['hiring_id'])));
			}
		}

		public function getvoiceurl(){
			$mp3url = getvoice($_GET['voice']);
			header('Content-type: audio/mp3');  
			header('Content-Disposition: attachment; filename="'.rand(1000000,9999999).'.mp3"');  
			readfile($mp3url);  
		}
	
		public function getposition(){
			$hid = $_POST['hid'];
			$position = D('position')->where('is_del = 0 AND hid = '.$hid)->select();
			exit(json_encode($position));
		}
		public function reply_other(){
			$send_id = $_POST['send_id'];
			$resume_id = D('send_resume')->where('id ='.$send_id)->getField('resume_id');
			$uid = D('resume')->where('resume_id ='.$resume_id)->getField('uid');
			$data['info_id'] = D('member_info')->where('uid ='.$uid)->getField('info_id');
			$cid = $_SESSION['company']['cid'];
			$data['uid'] = $cid;
			$data['phone'] = $_POST['phone'];
			$data['status'] = 1;
			$data['check'] = 0;
			$data['ctime'] = time();
			$send = D('send')->add($data);
			$id = mysql_insert_id();
			$url = "http://www.rencailaile.com/weixin.php?s=/Companycontrol/resume_list/reply_other/1/send_id/".$send_id."/cid/".$cid."/sid/".$id;
			$url = $this->short_url($url);
			$msg = '您的同事向您推荐了以下人才，请点击查看：'.$url.'。【人才来了】';
			$result = $this->SendMesg($msg,$_POST['phone']);
			if($result){
				$this->success('推荐成功');
			}else{
				$this->error('推荐失败');
			}
		}
		
		public function SendMesg($con,$num){
		require_once 'HttpClient.class.php';
		$client = new HttpClient ("www.lx198.com");
		if (! $client->post ( '/sdk/send', array (
				'accName' => "18621771369",
				'accPwd' => strtoupper ( md5 ( "dd200412" ) ),
				'bizId' => date ( 'YmdHis' ),
				'content' => mb_convert_encoding ( $con, 'UTF-8', 'UTF-8' ),
				'aimcodes' =>$num ,
				'dataType'=>"xml"
		) )) {
			return false;
		} else {
			$xmlStr = $client->getContent () ;
			$doc = new DOMDocument ();
			$doc->loadXML ( $xmlStr );
			$xml = simplexml_load_string($xmlStr);
			if($xml->replyCode == 1){
				return true;
			}else{
				return false;
			}
		}
	}
	
	public function short_url($url){
		$key = '1562966081'; 
   		$baseurl = 'http://api.t.sina.com.cn/short_url/shorten.json?source='.$key.'&url_long='.$url; 
   		$ch=curl_init(); 
	    curl_setopt($ch, CURLOPT_URL,$baseurl); 
	    curl_setopt($ch, CURLOPT_HEADER, 0); 
	    curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
	    $strRes=curl_exec($ch); 
	    curl_close($ch); 
	    $arrResponse=json_decode($strRes,true); 
	    if (isset($arrResponse->error) || !isset($arrResponse[0]['url_long']) || $arrResponse[0]['url_long'] == ''){
	    	return 0; 
	    } 
    	return $arrResponse[0]['url_short']; 
   
    }

    public function resume_one(){
    		$sid = $_GET['sid'];
			$uid = $_GET['uid'];
			$da['check'] = 1;
			D('send')->where('sid ='.$sid)->save($da);
			$myresume = D('member_info')->where('uid = '.$uid)->find();
			if(!empty($myresume)){
				$myjobexper = D('exper')->where(array('uid'=>$uid,'is_del'=>0))->findAll();
				$myresume['worklist'] = $myjobexper;
				$resume = D('resume')->where('uid = '.$uid)->find();
				$myresume['resume'] = $resume;
				$myresume['pic'] = get_img_url($myresume['face']);
			}
			$this->assign('resume',$myresume);
			$this->display();
	}

}
?>
